<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>订单支付</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../css/weui.css"/>
    <link rel="stylesheet" type="text/css" href="../css/weui.min.css"/>
    <link rel="stylesheet" type="text/css" href="../css/style.css"/>
    <script src="../script/jquery-3.2.1.js"></script>
    <script src="../script/switch.js"></script>
    <script src="../script/api.js"></script>
    <style>
        .empty{ text-align: center; padding: 120px 0; }
    </style>
</head>
<!-- 订单页面 -->
<body>
<div id="payment" style="position: fixed; width: 100%; height: 100%; z-index: 999; display: none;">
		<div style="position: absolute; width: 100%; height: 100%; background: #000000; opacity: 0.50"></div>
		<div class="weui-cells weui-cells_radio" style="position: absolute;bottom: 0; width: 100%">
			<label class="weui-cell weui-check__label" for="UnionPay">
				<div class="weui-cell__bd">
					<p>银联</p>
				</div>
				<div class="weui-cell__ft">
					<input type="radio" class="weui-check" name="radio1" id="UnionPay" value="UnionPay" checked="checked">
					<span class="weui-icon-checked"></span>
				</div>
			</label>
			<label class="weui-cell weui-check__label" for="Alipay">
				<div class="weui-cell__bd">
					<p>支付宝</p>
				</div>
				<div class="weui-cell__ft">
					<input type="radio" name="radio1" class="weui-check" id="Alipay" value="Alipay">
					<span class="weui-icon-checked"></span>
				</div>
			</label>
			<label class="weui-cell weui-check__label" for="WeChatPay">
				<div class="weui-cell__bd">
					<p>微信</p>
				</div>
				<div class="weui-cell__ft">
					<input type="radio" name="radio1" class="weui-check" id="WeChatPay" value="WeChatPay">
					<span class="weui-icon-checked"></span>
				</div>
			</label>
			<div class="weui-form-preview__ft">
				<button class="weui-form-preview__btn weui-form-preview__btn_primary" onclick="confirmPayment()">确定</button>
				<button class="weui-form-preview__btn" style="color: #000000;" onclick="cancelPayment()">取消</button>
			</div>
		</div>
	</div>
<div class="weui_cells_title">订单详情</div>
<div class="weui_cell">
</div>
<!-- <div class="weui_cells_title">业务号:</div>
<div class="weui_cell">
    <div class="weui_cell_bd weui_cell_primary" >
        <p type="text" id="ywid"></p>
    </div>
</div> -->
<!-- <div class="weui_cells_title">订单号:</div>
<div class="weui_cell">
    <div class="weui_cell_bd weui_cell_primary" >
         <p type="text" id="orderid"></p>
    </div>
</div> -->
<div class="weui_cells_title">技术服务费:</div>
<div class="weui_cell">
    <div class="weui_cell_bd weui_cell_primary" >
        <p id="price">10元</p>
    </div>
</div>
<!--
<div style="display: none">
    <div class="weui_cells_title">收款单位:</div>
    <div class="weui_cell">
       <div class="weui_cell_bd weui_cell_primary" >
          <p id="skdw"></p>
       </div>
    </div>
</div>
-->
<div class="weui_cells_title">开票信息:</div>
<div class="weui_cell">
    <div class="weui_cell_bd weui_cell_primary" >
        <p>是否开票:
            <a style="width: 10%">
                <button class="weui_btn weui_btn_primary"
                        style="background-color: #808080;width: 100%;font-size:8px;text-align: center" id="yes" onclick="yesfp()">是</button>
            </a>
            <a style="width: 10%">
                <button class="weui_btn weui_btn_primary"
                        style="background-color: #18b4ed;width: 100%;font-size:8px;text-align: center" id="no" onclick="nofp()">否</button>
            </a>
        </p>
        <div id="node">

        </div>
        <br/>
        <button id="button" class="weui_btn weui_btn_primary" style="background-color: #18b4ed;width: 60% "
                onclick="surePay()">确认支付</button>
    </div>
</div>

</body>
</html>
<script type="text/javascript">
    //加载页面时填写信息
    var tradeNO; 
    var id;
    var ip;
    var orderid;
    var msg;
    var name;
    var address;
    var payid;
    var account = $api.getStorage("dl_phone");
	var dl_phone =$api.getStorage("dl_phone");
    var content = {"phone":dl_phone};
    var ywid;
	var je = $('#price').text();
    apiready = function(){
    var demo = api.require('ipAddress');
		demo.getIp(
				function(ret, err) {
    			ip = ret.ip;
    			});
    	ywid = getQueryString("ywid");
    	$("#ywid").text(ywid);
        orderid = getQueryString("orderid");
        $("#orderid").text(orderid);
        msg = getQueryString("msg");
        name = getQueryString("name");
        address = getQueryString("address");
        //$("#skdw").text(name);
    };
	//支付按钮点击函数
    function surePay() {
        //$('#payment').css('display','block');
		//$('#UnionPay').prop('checked',true);
		getPayId();
    }
	// 确定支付
	function confirmPayment() {
		var value = $('#payment input[name="radio1"]:checked ').val();
		if (value == 'WeChatPay' || value == 'Alipay') {
			api.alert({ msg: '抱歉，该功能尚未开放' });
		} else {
			$('#payment').hide();
			
			getPayId(value);
			
		}
    }
	// 取消支付
	function cancelPayment() {
        $('#payment').css('display','none');
    }
    //获取支付号
    function getPayId(_value_) {
        var bmc = $('.fapiao').val();
        var bsh = $('.shuihao').val();
        var bdzdh = $('.dzdh').val();
        var bkhh = $('.khhzh').val();
        var bfplx = $('.fplx').val();
        var url = 'http://www.scscpt.com:8520/wzrz/payorder';
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'text',
            header:{
                'Content-Type':'application/x-www-form-urlencoded;charset=utf-8'
            },
            data: {
                values:{
                    openid: account,
                    je: je,
                    amc: name,
                    adh: account,
                    adz: address,
                    bmc: bmc,
                    bsh: bsh,
                    bdzdh: bdzdh,
                    bkhh: bkhh,
                    bfplx: bfplx,
                    orderid: orderid
                }
            }
        }, function(ret, err) {
            var obj = JSON.parse(ret);
            if (obj.msg == "Y") {
           	    payid = obj.payid;
                payOrder();		
            }else if(obj){
            	alert(obj.msg);
            		
            }else{
            	api.alert({ msg: JSON.stringify(err) });
            }
        });
    }
	// 银联支付
	function UnionPay(){
		var callback = function(ret, err){
			//$("#loadingToast").hide();
			
			
			console.log(ret.pd);
			api.alert({msg:ss});
			if(ret){
				if(JSON.stringify(ret) == "{}" || ret.pd == null || typeof(ret.pd.ZFPWZT) == "undefined"){
					api.alert({msg:'还未设置支付密码'});
					api.openWin({
						name: '个人信息',
						url: 'html/userinfo_edit.html'
					});
				}else{
					if(ret.pd.ZFPWZT=="1"){
						//$("#loadingToast").show();
						var dl_phone = $api.getStorage("dl_phone");
						var cardId = '';
						var callback = function(ret, err){
							//$("#loadingToast").hide();
							if(ret){
								if(ret.success == false){
									if(api.systemType=='ios'){
										api.confirm({
											title: '温馨提示',
											msg:'对不起，您还没绑定银行卡，请先绑定银行卡！',
											buttons: ['去绑卡', '暂时不用']
										}, function(ret, err) {
											var index = ret.buttonIndex;
											if(index==1){
												api.openWin({
													name: 'addCard',
													progress: {
														type: "default",                //加载进度效果类型，默认值为 default，取值范围为 default|page，default 等同于 showProgress 参数效果；为 page 时，进度效果为仿浏览器类型，固定在页面的顶部
														title: "",               //type 为 default 时显示的加载框标题
														text: "加载中...",                //type 为 default 时显示的加载框内容
														color: "#fff"                //type 为 page 时进度条的颜色，默认值为 #45C01A，支持#FFF，#FFFFFF，rgb(255,255,255)，rgba(255,255,255,1.0)等格式
													},
													url: 'https://wechat.scjinsui.com/weixin/wx/unionPay/card_add.jsp?dlPhone='+dl_phone
												});
											}else{
												//api.closeWin();
											}
										});
									}else{
										var r=confirm("您还没有绑卡哦，确定现在去绑卡吗？");
										if(r){
											api.openWin({
												name: 'addCard',
												progress: {
													type: "default",                //加载进度效果类型，默认值为 default，取值范围为 default|page，default 等同于 showProgress 参数效果；为 page 时，进度效果为仿浏览器类型，固定在页面的顶部
													title: "",               //type 为 default 时显示的加载框标题
													text: "加载中...",                //type 为 default 时显示的加载框内容
													color: "#fff"                //type 为 page 时进度条的颜色，默认值为 #45C01A，支持#FFF，#FFFFFF，rgb(255,255,255)，rgba(255,255,255,1.0)等格式
												},
												url: 'https://wechat.scjinsui.com/weixin/wx/unionPay/card_add.jsp?dlPhone='+dl_phone
											});
										}else{
											//api.closeWin();
										}
									}
								}else{
									payOrder();
									//已绑卡已设置支付密码
								}
							}else{
								api.alert({msg: JSON.stringify(err)});
							}
						}
						var content = {"phone":dl_phone};
						apiAjax(JSON.stringify(content),url.getCardId,callback);
					}else{
						api.alert({msg:'还未设置支付密码'});
						api.openWin({
							name: '个人信息',
							url: 'html/userinfo_edit.html'
						});
					}
				}
			}else{
				api.alert({msg:'网络异常'});
			}
		};
		encApiAjax(JSON.stringify(content),url.zfmmzt,callback);
	}
	// 支付宝支付
	function Alipay() {
		var aliPay = api.require('aliPay');
		aliPay.pay({
			subject: '职场生活商品交易',
			body: _tradeNO,
			amount: je,
			tradeNO: payid
		}, function(ret, err) {
			if(ret.code == 9000){
				api.alert({
					title: '支付结果',
					msg: '支付成功！',
					buttons: ['确定']
				});
				localStorage.setItem("ShoppingCart", "");
			}else if(ret.code == 6001){
				api.alert({
					title: '支付结果',
					msg: '取消支付。',
					buttons: ['确定']
				});
			}else{
				api.alert({
					title: '支付结果',
					msg: '支付异常...',
					buttons: ['确定']
				});
			}
		});
	}
  //获取IP地址
  //提交订单
    function payOrder() {
    //
        var url = 'http://www.scscpt.com:8520/wzrz/payorder2';
        var je = $('#price').text();
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'text',
            header:{
                'Content-Type':'application/x-www-form-urlencoded;charset=utf-8'
            },
            data: {
                values:{
                	ywid:ywid,
					orderid: orderid,
                    xfje: je,
                    payid: payid,
                    openid: account,
                    msg: msg,
                    dlPhone: account,
                    Je: je,
                    ip: ip,
                    deviceID: api.deviceId,
                }
            }
        }, function(ret, err) {
            var obj = JSON.parse(ret);
            if (obj.flag == 'Y') {
                api.showProgress({
   				style: 'default',
   				 animationType: 'fade',
   				 title: '银行扣款中...',
   				 text: '请耐心等待...',
   				 modal: false
			}); 
			getfkzt();               
			//window.location.href="../html/frame0.html";
            } else {
                api.alert({ msg: '支付失败，请重试' });
            }
        });
    }
    
    
    /*
     function surePay(){
     if (xhr) {
     xhr.open('POST', url, true);
     xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     xhr.onreadystatechange = handler;
     xhr.send("orderid="+id);
     } else {
     }
     }
     function handler(evtXHR) {
     if (xhr.readyState == 4) {
     if (xhr.status == 200) {
     var response = xhr.responseText;
     var msg = response.split('*');
     var re = msg[1];
     if("success"==re){
     alert("支付成功");
     window.location.href="../html/frame1.html";
     }
     } else {
     }
     }
     else {
     }
     }
     */

    //获取支付结果
    function getfkzt(){
    var url = 'http://www.scscpt.com:8520/wzrz/paysuccess';
		api.ajax({
            url: url,
            method: 'post',
            dataType: 'text',
            header:{
                'Content-Type':'application/x-www-form-urlencoded;charset=utf-8'
            },
            data: {
                values:{
                     ywid:ywid
                }
            }
        },function(ret,err){
        	var obj = JSON.parse(ret);
        	if(obj.flag == 'Y'){
             api.hideProgress();
             if(obj.spay= '-1'){
             api.alert({
					title: '支付结果',
					msg: obj.msg,
					buttons: ['确定']
				});
				window.location.href="../html/frame0.html";
				}else{
				api.alert({
					title: '支付结果',
					msg: obj.msg,
					buttons: ['确定']
				});
          		window.location.href="../html/frame0.html";
          		}
        	}else{
        		api.alert({
					title: '支付结果',
					msg: '支付异常...',
					buttons: ['确定']
				});
        	}
        	
        });
    }
    //根据按钮点击展示相应的发票填写
    function nofp(){
        $("#no").css({"background-color":"#18b4ed"});
        $("#yes ").css({"background-color":"#808080"});
        var box = document.getElementById("node");
        while(box.hasChildNodes()){
            box.removeChild(box.firstChild);
        }
    }
    function yesfp(){
        $("#yes").css({"background-color":"#18b4ed"});
        $("#no").css({"background-color":"#808080"});
        var box = document.getElementById("node");
        var li = document.createElement("div");
        while(box.hasChildNodes()){
            box.removeChild(box.firstChild);
        }
        li.style.textAlign = "left";
        li.style.marginLeft = "20px";
        li.innerHTML =
            '<div class="weui_cells_title">发票类型：电子发票</div>'+
            '<p>发票选择:'+
            '<a style="width:20%">'+
            '<button class="weui_btn weui_btn_primary" '+
            'style="background-color: #18b4ed;width: 100%;font-size:8px;'+
            'text-align: center" id="gr" onclick="grfp()">个人</button>'+
            '</a>'+
            '<a style="width:20%">'+
            '<button class="weui_btn weui_btn_primary"'+
            'style="background-color: #808080;width: 100%;font-size:8px;'+
            'text-align: center" id="qy" onclick="qyfp()">企业</button>'+
            '</a>'+
            '</p>'+
            '<div id="cnode">'+
            '<div>'+
            '<div class="weui_cells_title">发票抬头(必填):</div>'+
            '<div class="weui_cell">'+
            '<div class="weui_cell_bd weui_cell_primary" >'+
            '<input class="weui_input fapiao" type="text" placeholder="个人发票" id="grfptt"/>'+
            ' </div>'+
            '</div>'+
            '</div>'+
            '</div>'+
            '</div>';
        box.appendChild(li);
    }
    function grfp(){
        $("#gr").css({"background-color":"#18b4ed"});
        $("#qy").css({"background-color":"#808080"});
        var box = document.getElementById("cnode");
        while(box.hasChildNodes()){
            box.removeChild(box.firstChild);
        }
        var li = document.createElement("div");
        li.style.textAlign = "left";
        li.style.marginLeft = "20px";
        li.innerHTML =
            '<div>'+
            '<div class="weui_cells_title">发票抬头(必填):</div>'+
            '<div class="weui_cell">'+
            '<div class="weui_cell_bd weui_cell_primary" >'+
            '<input class="weui_input fapiao" type="text" placeholder="个人发票" id="grfptt"/>'+
            ' </div>'+
            '</div>'+
            '</div>';
        box.appendChild(li);
    };
    function qyfp(){
        $("#qy").css({"background-color":"#18b4ed"});
        $("#gr").css({"background-color":"#808080"});
        var box = document.getElementById("cnode");
        while(box.hasChildNodes()){
            box.removeChild(box.firstChild);
        }
        var li = document.createElement("div");
        li.style.textAlign = "left";
        li.style.marginLeft = "20px";
        li.innerHTML =
            '<div>'+
            '<div class="weui_cells_title">发票抬头(必填):</div>'+
            '<div class="weui_cell">'+
            '<div class="weui_cell_bd weui_cell_primary" >'+
            '<input class="weui_input fapiao" type="text" placeholder="单位发票" id="grfptt"/>'+
            ' </div>'+
            '</div>'+
            '<div class="weui_cells_title shuihao">税号:</div>'+
            '<div class="weui_cell">'+
            '<div class="weui_cell_bd weui_cell_primary" >'+
            '<input class="weui_input" type="text" placeholder="" id="grfptt"/>'+
            ' </div>'+
            '</div>'+
            '<div class="weui_cells_title dzdh">地址电话:</div>'+
            '<div class="weui_cell">'+
            '<div class="weui_cell_bd weui_cell_primary" >'+
            '<input class="weui_input" type="text" placeholder="" id="grfptt"/>'+
            ' </div>'+
            '</div>'+
            '<div class="weui_cells_title khhzh">开户行账号:</div>'+
            '<div class="weui_cell">'+
            '<div class="weui_cell_bd weui_cell_primary" >'+
            '<input class="weui_input fplx" type="text" placeholder="" id="grfptt"/>'+
            ' </div>'+
            '</div>'+
            '</div>';
        box.appendChild(li);
    };

</script>
